﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Xmobile.Users;
using LogCMS.DAL;
using System.ComponentModel;

namespace Portal.BO.Editoral.Statistic
{
    public static class StatPostContentByUser
    {
        [DataObjectMethod(DataObjectMethodType.Select)]
        public static DataTable GetListUser()
        {

            DataTable dtUser;
            DataTable objresult = new DataTable();
            DataTable dtz = new DataTable();
            object[] objarr = null;
            
            
            LogCMS.DAL.MainDB objDbDal = new LogCMS.DAL.MainDB();
            
            using (Xmobile.Users.MainDB objDb = new Xmobile.Users.MainDB())
            {
               
                //dtUser = objDb.UserCollection.GetAsDataTable(" User_isActive = 1 ", "", startIndex, PageSize, ref abc);
                dtUser = objDb.StoreProcedure.User_GetListByWhereForPaging(" Where Channel_ID = " + Portal.API.Config.CurrentChannel);
                
                // Add them mot so column ve thong ke
                dtUser.Columns.Add(new DataColumn("Wait_Edit_Content_Count"));
                dtUser.Columns.Add(new DataColumn("Wait_Approve_Content_Count"));
                dtUser.Columns.Add(new DataColumn("Approved_Content_Count"));

                objresult = dtUser.Clone();
                string startDate = "20070101";
                string endDate = DateTime.Today.AddDays(1).ToString("yyyyMMdd");
                
                // Bind du lieu vao  DataTable
                foreach (DataRow objrow in dtUser.Rows) 
                {
                    objarr = objrow.ItemArray;
                    
                    // Lấy số bài viết của user đang ở status = Wait_Edit_Content
                    dtz = objDbDal.StoredProcedures.Stats_GetReportSum(endDate, "", ((int)CountKey.User_Wait_Edit_Content).ToString(), startDate, "'" + objrow[0].ToString() + "'", "");
                    if (dtz.Rows.Count > 0)
                        objarr[objarr.Length - 3] = dtz.Rows[0][0].ToString();
                    else
                        objarr[objarr.Length - 3] = "0";

                    // Lấy số bài viết của user đang ở status = Wait_Approve_Content
                    dtz = objDbDal.StoredProcedures.Stats_GetReportSum(endDate, "", ((int)CountKey.User_Wait_Approve_Content).ToString(), startDate, "'" + objrow[0].ToString() + "'", "");
                    if (dtz.Rows.Count > 0)
                        objarr[objarr.Length - 2] = dtz.Rows[0][0].ToString();
                    else
                        objarr[objarr.Length - 2] = "0";

                    // Lấy số bài viết của user đang ở status = Approved_Content
                    dtz = objDbDal.StoredProcedures.Stats_GetReportSum(endDate, "", ((int)CountKey.User_Approved_Content).ToString(), startDate, "'" + objrow[0].ToString() + "'", "");
                    if (dtz.Rows.Count > 0)
                        objarr[objarr.Length - 1] = dtz.Rows[0][0].ToString();
                    else
                        objarr[objarr.Length - 1] = "0";

                    objresult.Rows.Add(objarr);
                }
            }

            dtUser.Dispose();
            return objresult;
        }

        [DataObjectMethod(DataObjectMethodType.Select)]
        public static DataTable getPage(int numPage)
        {
            int intPagenum = numPage;
            DataTable objTb = new DataTable();
            objTb.Columns.Add(new DataColumn("Text", typeof(string)));
            objTb.Columns.Add(new DataColumn("Value", typeof(string)));
            for (int i = 1; i <= intPagenum; i++)
            {
                DataRow myRow = objTb.NewRow();
                myRow["Text"] = i.ToString();
                myRow["Value"] = Convert.ToString(i - 1);
                objTb.Rows.Add(myRow);
            }
            if (intPagenum == 0)
            {
                DataRow myRow = objTb.NewRow();
                myRow["Text"] = "1";
                myRow["Value"] = "0";
                objTb.Rows.Add(myRow);
            }
            return objTb;
        }

        public static int GetRowsCount()
        {
            
            DataTable objresult;
            using (Xmobile.Users.MainDB objdb = new Xmobile.Users.MainDB())
            {
                objresult = objdb.StoreProcedure.vc_Execute_Sql(" Select count(*) From [User] Where  User_isActive = 1 ");
            }
            return Convert.ToInt32(objresult.Rows[0][0]);
        }
        
    }
}
